// pages/self/add-ress/add-ress.js
const httpNet = require("../../../utils/httpUtil.js");
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.js');
var _key = "AFBBZ-26436-QYCSL-ESUEC-SGGWJ-KEBAS";
var qqmapsdk;
qqmapsdk = new QQMapWX({
    key: _key
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressmap: '',
        admin: {
            userName: '',
            mobilePhone: '',
            gender: '1'
        },
        userRess: {

            province: '',
            city: '',
            area: '',
            addressInfo: '',
            longitude: '',
            latitude: '',
        },
        isedit: 0,
        isclick: 0,
        please: 1,
    },
    addressMap: function(e) {
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                console.log(res);
				var latlng ={};
				latlng.latitude = res.latitude;
				latlng.longitude= res.longitude;
				console.log(latlng);
                qqmapsdk.reverseGeocoder({
                    location: latlng,
                    success: function(data) {
						console.log(data);
                        that.setData({
                            userRess: {
                                province: data.result.address_component.province,
                                city: data.result.address_component.city,
                                area: data.result.address_component.district,
                                addressInfo: res.name,
                                longitude: res.longitude,
                                latitude: res.latitude,
                            },
                            please: 0
                        });
						console.log(that);
                    },
                    fail: function(data) {
                        console.log(data);
                    }
                })
return;
                // 根据返回的经纬度解析当前地址所在的省市区
                qqmapsdk.geocoder({
                    address: res.address,
                    success: function(data) {
                        console.log(data);
                        that.setData({
                            userRess: {
                                province: data.result.address_components.province,
                                city: data.result.address_components.city,
                                area: data.result.address_components.district,
                                addressInfo: res.name,
                                longitude: res.longitude,
                                latitude: res.latitude,
                            },
                            please: 0
                        });
                    },
                    fail: function(data) {
                        console.log(data);

                    }
                })
            },
            fail: function(res) {
                console.log(res)
            },
        })
    },
    formSubmit: function(e) {
        console.log(this.data);
        console.log(e.detail.value);
        const that = this;
        const phoneRexp = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
        // 校验参数是否为空
        if (that.data.isclick == 0) {
            that.setData({
                isclick: 1
            });
            if (e.detail.value.name == "") {
                wx.showToast({
                    title: '请输入您的姓名',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (e.detail.value.phone == "") {
                wx.showToast({
                    title: '请输入您的手机号',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (e.detail.value.phone.length != 11) {
                wx.showToast({
                    title: '您的手机号有误',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (!phoneRexp.test(e.detail.value.phone)) {
                wx.showToast({
                    title: '手机号格式有误',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            if (e.detail.value.address == "") {
                wx.showToast({
                    title: '请输入您的地址',
                    icon: 'none',
                    duration: 2000
                });
                that.setData({
                    isclick: 0
                });
                return;
            };
            const myId = that.data.userId;
            let urls = '';
            let dataParam = {};
            if (!that.data.userRess.province) {
                wx.showToast({
                    title: '请填写收货地址',
                    icon: 'none',
                    duration: 2000
                })
                that.setData({
                    isclick: 0
                });
                return;
            }
            if (that.data.isedit === 0) {
                console.log(1);
                urls = 'xwx/addUserAddress';
                dataParam = {
                    userId: myId,
                    userName: e.detail.value.name,
                    mobilePhone: e.detail.value.phone,
                    province: that.data.userRess.province,
                    city: that.data.userRess.city,
                    area: that.data.userRess.area,
                    addressInfo: e.detail.value.homeRess,
                    longitude: that.data.userRess.longitude,
                    latitude: that.data.userRess.latitude,
                    gender: e.detail.value.gender
                }
            } else {
                urls = 'xwx/updateUserAddres';
                dataParam = {
                    userId: myId,
                    id: that.data.admin.id,
                    userName: e.detail.value.name,
                    mobilePhone: e.detail.value.phone,
                    province: that.data.userRess.province,
                    city: that.data.userRess.city,
                    area: that.data.userRess.area,
                    addressInfo: e.detail.value.homeRess,
                    longitude: that.data.userRess.longitude,
                    latitude: that.data.userRess.latitude,
                    gender: e.detail.value.gender
                };
            };
            httpNet.reqPost(urls, dataParam,
                function(datares) {
                    wx.hideLoading();
                    console.log(datares)
                    if (datares.resultCode === 1000) {
                        var pages = getCurrentPages(); // 获取页面栈
                        var currPage = pages[pages.length - 1]; // 当前页面
                        var prevPage = pages[pages.length - 2]; // 上一个页面
                        prevPage.setData({
                            isBack: {
                                back: 1
                            }
                        });
                        that.setData({
                            isclick: 0
                        });
                        wx.navigateBack({
                            delta: 1
                        })
                    } else {
                        that.setData({
                            isclick: 0
                        });
                        wx.showToast({
                            title: '新增地址失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const that = this;
        if (options.id) {
            console.log(options.id)
            that.setData({
                userRess: {
                    province: options.province,
                    city: options.city,
                    area: options.area,
                    addressInfo: options.addressInfo,
                    longitude: options.longitude,
                    latitude: options.latitude,
                },
                admin: {
                    id: options.id,
                    userName: options.userName,
                    mobilePhone: options.mobilePhone,
                    gender: options.gender
                },
                isedit: 1
            });
        };
        // 获取用户id
        wx.getStorage({
            key: 'uid',
            success: function(res) {
                console.log(res)
                that.setData({
                    userId: res.data
                })
            },
        })
        console.log(this.data.editress)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        console.log(this.data)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})